;$(function($) {
    /**
     * 发送短信
     */
    $(".YPGetCode").on("click", function() {
        var wait    = $(this).attr("data-wait");
        if (undefined == wait || 0 == wait)
        {
            var mobile  = $("#mobile_phone").val();
            var url     = $(this).attr("data-url");
            if ('' == mobile)
            {
                return showWechatMsg("温馨提示", "请输入手机号码", "", "alert");
            }
            if (0 == isMobile(mobile))
            {
                return showWechatMsg("温馨提示", "手机号码格式错误", "", "alert");
            }

            $(this).attr("style", "background: #808080;");
            $(this).attr("data-wait", 1);
            countDown(this, 60);

            var params  = new Object();
            params.mobile   = mobile;
            params.type     = type;
            Ajax.call(url, params, callbackResponse, "POST", "JSON");
        }
    });

    $(".YPSKU").on("click", function () {
        var image   = $(this).attr("data-img");
        //显示遮罩层
        ShowDIV('ShowSKU');
        //统一置顶
        $('body,html').animate({ scrollTop: 0 }, 200);

        var html  = "<div id='ShowSKU' class='sku-layout sku-box-shadow' style='overflow: hidden; bottom: 0px; width: 100%; max-width: 640px; visibility: visible; height: 258px; position: absolute; z-index: 1000; transform: translate3d(0px, 0px, 0px); transition: all 300ms ease; opacity: 1; background: white;'>";
        html    += "<div class='layout-title sku-box-shadow name-card sku-name-card' id='skubox'>";
        html    += "<div class='thumb'><img src='" + image + "' ></div>";
        html    += "<div class='detail goods-base-info'>";
        html    += "<p class='title c-black ellipsis'>首创香薰手机支架</p>";
        html    += "<div class='goods-price'>";
        html    += "<div class='current-price pull-left c-black'>";
        html    += "<span class='price-name pull-left font-size-14 c-orange'>￥</span><i class='price font-size-18 vertical-middle c-orange'>88.00</i></div>";
        html    += "</div>";
        html    += "</div>";
        html    += "<div class='sku-cancel'>";
        html    += "<div id='cancel-img' class='cancel-img'></div>";
        html    += "</div>";
        html    += "</div>";
        html    += "<div class='layout-content' style='max-height: 468px;'>";
        html    += "<div class='goods-models block block-list border-top-0'><dl class='clearfix block-item'><dt class='model-title sku-sel-title'>";
        html    += "<label>颜色：</label>";
        html    += "</dt>";
        html    += "<dd>";
        html    += "<ul class='model-list sku-sel-list'>";
        html    += "<li class='tag sku-tag pull-left ellipsis' value='0'>黑</li>";
        html    += "<li class='tag sku-tag pull-left ellipsis' value='0'>白</li>";
        html    += "<li class='tag sku-tag pull-left ellipsis' value='0'>玫瑰金</li>";
        html    += "</ul>";
        html    += "</dd></dl>";
        html    += "<dl class='clearfix block-item'><dt class='model-title sku-num pull-left'>";
        html    += "<label>数量</label></dt><dd><dl class=''>";
        html    += "<div class='quantity'>";
        html    += "<button class='minus disabled' type='button' disabled='true'></button>";
        html    += "<input type='text' class='txt' value='1'>";
        html    += "<button class='plus' type='button'></button>";
        html    += "</div>";
        html    += "<div class='stock pull-right font-size-12'></div>";
        html    += "</dl></dd></dl></div>";
        html    += "<div class='confirm-action content-foot clearfix'>";
        html    += "<div class='half-button'>";
        html    += "<a href='javascript:;' class='confirm btn btn-block btn-orange-dark'>立即购买</a>";
        html    += "</div>";
        html    += "</div>";
        html    += "</div>";
        html    += "</div>";

        var sku = $("#ShowSKU");
        sku.length > 0 && (sku.remove());

        $("body").append($(html)) && $("#skubox").fadeIn("slow");

        //购买页面的选择颜色事件
        $(".sku-layout .model-list li").on("click", function () {
            // 由未选中变为选中状态
            if($(this).val() == 0) {
                $(this).removeClass("tag").addClass("tag-orangef60");
                $(this).addClass("active");
                $(this).val(1);
            } else {// 由选中变为未选中状态
                $(this).removeClass("tag-orangef60").addClass("tag");
                $(this).removeClass("active")
                $(this).val(0);
            }
        });


        var textval = $(".txt").val();
        $(".plus").on("click",function(){
            textval++;
            //textval = textval + 1;
            $(".txt").val(textval);
            if(textval > 1) {
                $(".minus").removeClass("disabled")
                $(".minus").removeAttr("disabled"); //移除disabled属性
            }
        });
        $(".minus").on("click",function(){
            textval--;
            //textval = textval - 1;
            $(".txt").val(textval);
            if(textval == 1) {
                $(".minus").addClass("disabled");
                $(".minus").attr('disabled',"true");//添加disabled属性

            }
        });


        //购买页面的点击关闭事件
        $("#cancel-img").on("click", function () {
            $('#ShowSKU').hide();
            //隐藏遮罩层
            closeDiv("ShowSKU");
        });
    });

    $(".YPWechatRZMsg").on("click", function () {
        var msg     = $(this).attr("data-msg");

        var html    = "<div class='weui_dialog_alert' id='YPWechat'>";
        html        += "<div class='weui_mask'></div>";
        html        += "<div class='weui_dialog'>";
        html        += "<div class='weui_dialog_hd'><strong class='weui_dialog_title'></strong></div>";
        html        += "<div class='weui_dialog_bd'>" + msg + "</div>";
        html        += "<div class='weui_dialog_ft'>";
        html        += "<a href='javascript:;' class='weui_btn_dialog primary YPOK'>我知道了</a>";
        html        += "</div>";
        html        += "</div>";
        html        += "</div>";

        $("#YPWechat").length > 0 && $("#YPWechat").remove();
        $("body").append($(html)) && $("#YPWechat").fadeIn("slow");

        $(".YPOK").one("click", function() {
            $("#YPWechat").length > 0 && $("#YPWechat").remove();
        });
    });


});

//遮罩层
function ShowDIV(thisObjID) {
    $("#BgDiv").css({ display: "block", height: $(document).height() });
    var yscroll = document.documentElement.scrollTop;
    $("#" + thisObjID ).css("top", "100px");
    $("#" + thisObjID ).css("display", "block");
    document.documentElement.scrollTop = 0;
    $("body").eq(0).css("overflow","hidden");
}

function closeDiv(thisObjID) {
    $("#BgDiv").css("display", "none");
    $("#" + thisObjID).css("display", "none");
    $("body").eq(0).css("overflow","scroll");
}


function callbackResponse(result)
{
    if (0 == result.error)
    {
        showToast(result.msg);
        if (undefined != result.callback)
        {
            window.location.href    = result.callback;
        }
    }
    else
    {
        showToast(result.msg);
    }
}

/**
 * countDown 倒计时
 * 
 * @param that $that 
 * @param countdown $countdown 
 * @access public
 * @return void
 */
function countDown(that, countdown)
{
    if (countdown == 0)
    {
        $(that).parent().find("a").text("重新获取验证码");
        $(that).attr("style", "");
        $(that).attr("data-wait", 0);
        return;
    }
    else
    {
        $(that).parent().find("a").text(countdown + " 秒后重新发送");
        countdown--;
    }

    setTimeout(function() {
        countDown(that, countdown);
    }, 1000);
}

/***********************************************/
//-- 公共函数
/***********************************************/
function showToast(a)
{
    if (a)
    {
        var b = $("#toast");
        b.length > 0 && (clearTimeout(b.attr("timeId")), b.remove()), $("body").append('<div id="toast" style="z-index:100000;max-width: 640px;width: 100%;text-align: center;overflow: hidden;position: fixed; bottom: 15%;font-size: 15px; line-height: 20px;color: #ffffff;"><span style="margin-right: 20px; margin-left: 20px;border-radius:30px;-webkit-border-radius: 30px;-moz-border-radius: 30px;display: inline-table;padding: 10px 18px;background: #000000;opacity: 0.9;filter:alpha(opacity=0.9);">' + a + "</span></div>"), $("#toast").fadeIn("slow"), $("#toast").attr("timeId", setTimeout(function() {
            $("#toast").fadeOut("slow")
        }, 3e3))
    }
}

function showWechatToast(a)
{
    if (a)
    {
        var b = $("#toast");
        b.length > 0 && (clearTimeout(b.attr("timeId")), b.remove()), $("body").append("<div id='toast'><div class='weui_mask_transparent'></div><div class='weui_toast'><i class='weui_icon_toast'></i><p class='weui_toast_content'>" + a + "</p></div></div>"), $("#toast").fadeIn("slow"), $("#toast").attr("timeId", setTimeout(function() {
            $("#toast").fadeOut("slow")
        }, 3e3))
    }
}

function showWechatMsg(title, msg, url, type)
{
    var html    = "<div class='weui_dialog_" + type + "' id='YPWechat'>";
    html        += "<div class='weui_mask'></div>";
    html        += "<div class='weui_dialog'>";
    html        += "<div class='weui_dialog_hd'><strong class='weui_dialog_title'>" + title + "</strong></div>";
    html        += "<div class='weui_dialog_bd'>" + msg + "</div>";
    html        += "<div class='weui_dialog_ft'>";
    if ("confirm" == type)
    {
        html        += "<a href='javascript:;' class='weui_btn_dialog default YPCancel'>取消</a>";
    }
    html        += "<a href='javascript:;' class='weui_btn_dialog primary YPOK'>确定</a>";
    html        += "</div>";
    html        += "</div>";
    html        += "</div>";

    $("#YPWechat").length > 0 && $("#YPWechat").remove();
    $("body").append($(html)) && $("#YPWechat").fadeIn("slow");

    $(".YPCancel").one("click", function() {
        $("#YPWechat").length > 0 && $("#YPWechat").remove();
    });

    if (undefined != url && '' != url)
    {
        $(".YPOK").one("click", function() {
            Ajax.call(url, "", callbackResponse, "GET", "JSON");
        });
    }
    else
    {
        $(".YPOK").one("click", function() {
            $("#YPWechat").length > 0 && $("#YPWechat").remove();
        });
    }
}

function isMobile(text)
{
    var _emp = /^\s*|\s*$/g;
    text = text.replace(_emp, "");
    var _d = /^1[3578][01379]\d{8}$/g;
    var _l = /^1[34578][01256]\d{8}$/g;
    var _y = /^(134[012345678]\d{7}|1[34578][012356789]\d{8})$/g;
    if (_d.test(text))
    {
        return 3;
    }
    else if (_l.test(text))
    {
        return 2;
    }
    else if (_y.test(text))
    {
        return 1;
    }

    return 0;
}
